<template>
  <div class="patient-edit">
	  <core :value="value" @on-value-change="updateFormValue" ref="core"></core>
	  <form-footer @on-submit="submit" @on-cancel="cancel" class="footer"></form-footer>
  </div>
</template>

<script>
import core from "./core";
import footer from "../../components/form/footer";
import { updateUser, getUser } from "../../utils/actions";
export default {
	name: "patient-edit",
	props: {
		id: {
			type: [Number, String],
			default: "5aa8fc28f9054a000636b775"
		}
	},
	components: {
		core,
		[footer.name]: footer
	},
	data() {
		return {
			patientInfoValue: "",
			value: undefined
		};
	},
	methods: {
		updateFormValue(patientInfoValue) {
			this.patientInfoValue = patientInfoValue;
			console.log("patientInfoValue", patientInfoValue);
		},
		submit(next) {
			this.$refs.core
				.validate()
				.then(_ => {
					//提交
					updateUser(this.id, this.patientInfoValue)
						.then(res => {
							this.$message.success("操作成功！");
							//提交住院基本信息
							next();
						})
						.catch(err => {
							next();
							this.$message.error(`创建失败！${err.message}`);
						});
				})
				.catch(err => {
					next();
				});
		},
		cancel() {
			history.go(-1);
		},
		//加载用户数据
		load() {
			//获取数据
			getUser(this.id) //"5aaa1c15f9054a00093fa928"
				.then(res => {
					res = _.isString(res) ? JSON.parse(res) : res;
					this.value = res.patientInfo;
					console.log("patient-info", JSON.stringify(this.value));
				})
				.catch(err => {
					this.$message.error("获取用户信息失败");
				});
		}
	},
	created() {
		this.load();
	}
};
</script>

<style lang="less">
.patient-edit {
	.footer {
		text-indent: 10rem;
	}
}
</style>

